<template>
	<div class="new-page" :style="`min-height: ${pageMinHeight}px`">
		<a-button type="primary" style="float: left;margin-bottom: 10px;z-index: 999;" @click="add()">添加</a-button>
		<a-table :dataSource="lists" :columns="columns">
			<template slot="opera" slot-scope="row">
				<a-space>
					<a-button type="primary" @click="edit(row)">编辑</a-button>
					<a-button type="danger" @click="deletes(row.orgId)">删除</a-button>
				</a-space>
			</template>
		</a-table>
		<!--编辑-->
		<edit-model ref="editRef" @ok="editSuccess" />
		<!--编辑弹-->
		<!--添加-->
		<add-model ref="addRef" @ok="addSuccess" />
		<!--添加-->
	</div>
</template>

<script>
	import { message } from 'ant-design-vue';
	import { mapState } from 'vuex'
	import { orgLists, del } from "@/services/v2"
	import editModel from "./components/edit"
	import addModel from "./components/add"
	export default {
		name: 'Organization',
		components: {
			editModel,
			addModel
		},
		data() {
			return {
				lists: [],
				columns: [
					{
						title: '组织id',
						dataIndex: 'orgId',
						key: 'orgId',
					},
					{
						title: '名称',
						dataIndex: 'orgName',
						key: 'orgName',
					},
					{
						title: '操作',
						key: 'opera',
						scopedSlots: { customRender: 'opera' },
					},
				],
			}
		},
		computed: {
			...mapState('setting', ['pageMinHeight']),
		},
		created() {
			//页面初始化
			this.pageInit();
		},
		methods: {

			//页面初始化
			pageInit: function () {
				//获取列表
				this.getLists();
				return false;
			},

			getLists: function () {
				let self = this;
				orgLists().then((ref) => {
					ref.data.forEach(function (v) {
						v['key'] = v.orgId;
						self.lists.push(v);
					});
				});
				return false;
			},

			//添加组织
			add: function () {
				let self = this;
				//打开编辑弹窗
				self.$refs.addRef.open();
				return false;
			},

			//添加成功子组件向父组件通信
			addSuccess: function () {
				let self = this;
				self.$router.go(0);
				return false;
			},

			//编辑组织
			edit: function (row = '') {
				let self = this;
				//打开编辑弹窗
				self.$refs.editRef.open(row);
				return false;
			},

			//编辑组织子组件向父组件通信
			editSuccess: function () {
				let self = this;
				self.$router.go(0);
				return false;
			},

			//删除组织
			deletes: function (orgId = 0) {
				let self = this;
				self.$confirm({
					title: '系统提示',
					content: `是否确认删除？`,
					okButtonProps: {
						color: this.themeColor,
					},
					onOk() {
						del(orgId).then((ref) => {
							if (ref.code != 0) {
								return message.warning(ref.msg);
							}
							message.success('删除成功', 2, function () {
								self.$router.go(0);
							});

						});
					}
				});
			},

		}
	}
</script>

<style scoped lang="less">
</style>